﻿using Microsoft.Xna.Framework.Content;
using Imaginecup.util.resourceMan;
using Microsoft.Xna.Framework.Graphics;
using Imaginecup.sprite;
using Microsoft.Xna.Framework;
using imaginecup.util;
using Imaginecup.util;
using Imaginecup.factory;
using Imaginecup.sprite.animation;
using Imaginecup.gameState.scroll;
using Imaginecup.gameState.intro;
using Imaginecup.util.collision;

namespace Imaginecup.gameState.temp
{
    public class GameIntro : GameState
    {
        enum IntroState { DelayTime , Scroll, BoxPress ,  BoxAni , Fade_Out, Map }

        private ContentManager m_Content;

        private static GameIntro m_GameIntro = null;

        private ScrollMan m_ScrollMan;
        
        private FadeHelper m_FadeHelper;
        private ISprite m_FadeSprite;

        private AnimationIndexMan m_AnimationMan;
        private ISprite m_MapSprite;

        private AnimationIndexMan m_BoxAnimationMan;
        private ISprite m_BoxSprite;
        private Vector2 m_BoxPosition = new Vector2(255 , 2335);
        private Vector2 m_BoxSize = new Vector2(350, 277);

        private ISprite m_BoxPressBtn;
        private AnimationIndexMan m_PressBtnAnimationMan;
        private EntityInfo m_BoxPressInfo;


        private EntityInfo m_SkipInfo;
        private ISprite m_SkipSprite;

        private DelayUtil m_DelayMan;

        private IntroState m_State;

        private GameIntro()
        {
            m_Content = new ContentManager(m_Game.Services);
            m_Content.RootDirectory = "Content";

            m_ScrollMan = new ScrollMan();

        }

        public static GameIntro GetGameIntro()
        {
            if (m_GameIntro == null)
            {
                m_GameIntro = new GameIntro();
            }
            return m_GameIntro;
        }

        /************************************************************************/
        /* 초기화 되는 값                                                       */
        /************************************************************************/
        protected override void InitState()
        {
            m_State = IntroState.DelayTime;

            m_SkipInfo = new EntityInfo(650 , 400 , 131 , 59);
            
            m_BoxPressInfo = new EntityInfo( m_BoxPosition.X + 150 , m_BoxPosition.Y  , 100 , 115);

            IScrollProcess scroll = new HeightUpScroll(3540 - 480 , 100);
            m_ScrollMan.SetScrollProcess(scroll);
            m_ScrollMan.StartScroll();

            m_DelayMan = new DelayUtil(0.5);

            Vector2 v = new Vector2();

             Vector2.Transform(v, Matrix.CreateRotationZ(30));
        }


        /************************************************************************/
        /* ContentMan에 리소스 로드(배경,메뉴 리소스)                                  */
        /************************************************************************/
        protected override void LoadStateResource()
        {
            LoadScrollSprite();
            LoadFadeSprite();
            LoadMapSprite();
            LoadBoxSprite();

            ResourceMan.GetResourceMan().SetTextureFrame("Skip", m_Content.Load<Texture2D>("intro\\story_skip"));
            NormalSprite sprite = new NormalSprite();
            sprite.SetTextureName("Skip");

            m_SkipSprite = sprite;
        }

        private void LoadBoxSprite()
        {
            ResourceMan resourceMan = ResourceMan.GetResourceMan();
            resourceMan.SetTextureFrame("Box", m_Content.Load<Texture2D>("intro\\Box"), m_BoxSize);
            resourceMan.SetTextureFrame("BoxBtn", m_Content.Load<Texture2D>("intro\\story_box_click"), m_BoxPressInfo.mSize);

            m_BoxAnimationMan = new AnimationIndexMan();
            m_BoxAnimationMan.SetIndex(new Vector2(0, 0), 2);
            m_BoxAnimationMan.SetDelay(0.3);

            AnimationSprite animationSprite = new AnimationSprite(m_BoxAnimationMan);
            animationSprite.SetTextureName("Box");

            m_BoxSprite = animationSprite;

            m_PressBtnAnimationMan = new AnimationIndexMan();
            m_PressBtnAnimationMan.SetIndex(new Vector2(0, 0), 2);
            m_PressBtnAnimationMan.SetDelay(0.3);

            animationSprite = new AnimationSprite(m_PressBtnAnimationMan);
            animationSprite.SetTextureName("BoxBtn");

            m_BoxPressBtn = animationSprite;

        }

        private void LoadScrollSprite()
        {
            ResourceMan resourceMan = ResourceMan.GetResourceMan();
            resourceMan.SetTextureFrame("1", m_Content.Load<Texture2D>("intro\\Intro1"));
            resourceMan.SetTextureFrame("2", m_Content.Load<Texture2D>("intro\\Intro2"));

            WorldSprite sprite = new WorldSprite();

            sprite.SetMax(new Vector2(1, 2));
            for (int i = 1; i < 3; i++)
            {
                sprite.AddTextureName("" + i);
            }

            sprite.SetSourceInfoClass(m_ScrollMan);
            m_ScrollMan.SetSprite(sprite);
        }
        private void LoadFadeSprite()
        {
            m_FadeHelper = new FadeHelper();

            FadeSprite fadeSprite = new FadeSprite();
            fadeSprite.SetColorClass(m_FadeHelper);

            m_FadeSprite = fadeSprite;
        }

        private void LoadMapSprite()
        {
            m_AnimationMan = new AnimationIndexMan();
            m_AnimationMan.SetIndex(new Vector2(0, 0), 5);
            m_AnimationMan.SetDelay(0.125);

            AnimationSpriteByTexture animationSprite = new AnimationSpriteByTexture(m_AnimationMan);
            animationSprite.AddTextureName("intro\\Map1");
            animationSprite.AddTextureName("intro\\Map2");
            animationSprite.AddTextureName("intro\\Map3");
            animationSprite.AddTextureName("intro\\Map4");
            animationSprite.AddTextureName("intro\\Map5");

            m_MapSprite = animationSprite;
        }

        /************************************************************************/
        /* 리소스해제                                                                     */
        /************************************************************************/
        protected override void UnloadResource()
        {
            m_ScrollMan.Release();
            m_FadeSprite.Release();
            m_MapSprite.Release();
            m_BoxPressBtn.Release();
            m_BoxSprite.Release();
            ResourceMan.GetResourceMan().Release();
            m_Content.Unload();

            SoundMan.GetSoundMan().ReleaseSound();

        }

        public override void Update(Microsoft.Xna.Framework.GameTime gameTime)
        {
            if( m_State == IntroState.DelayTime)
            {
               if( m_DelayMan.IsProperTime(gameTime) )
                    m_State = IntroState.Scroll;
            }
            else if (m_State == IntroState.Scroll)
            {
                ScrollUpdate(gameTime);
            }

            else if( m_State == IntroState.Fade_Out )
            {
                FadeUpdate(gameTime);
            }

            else if( m_State == IntroState.Map )
            {
                MapUpdate(gameTime);
            }

            else if( m_State == IntroState.BoxPress )
            {
                BoxPressUpdate(gameTime);
            }

            else if( m_State == IntroState.BoxAni )
            {
                BoxUpdate(gameTime);
            }
        
            IMouseInputHandler mouseInput = MouseInputHandler.GetInputHandler();
            if( mouseInput.IsOneClick() && Collision.CollisionByPoint(m_SkipInfo , mouseInput.Position))
            {
                ChangeState((IGameState)StateFactory.GetStateFactory().GetObject(StateFactoryID.State_ChoiceMap));
                return;
            }
            base.Update(gameTime);
        }

        private void ScrollUpdate(GameTime gameTime)
        {
            m_ScrollMan.Update(gameTime);

            int tempCurrentY = (int)m_ScrollMan.GetCurrentStartPos().Y;
            int boxY = (int)m_BoxPosition.Y - tempCurrentY; // 480의 반 (m_BoxPosition이 중간이 되게 하기 위해

            if (boxY > 0 && boxY < 180 && m_BoxAnimationMan.IsLastIndex() == false)
            {

                m_State = IntroState.BoxPress;
                m_PressBtnAnimationMan.StartAnimation();
            }

            else if (m_ScrollMan.IsScrollEnd())
            {
                m_State = IntroState.Fade_Out;
                m_FadeHelper.SetFadeMode(RenderMode.FADE_OUT);
                m_FadeHelper.SetTime(3);
            }
        }

        private void BoxPressUpdate(GameTime gameTime)
        {
            IMouseInputHandler mouseInput = MouseInputHandler.GetInputHandler();

            Vector2 boxPositioninScreen = new Vector2(m_BoxPosition.X, m_BoxPosition.Y - m_ScrollMan.GetCurrentStartPos().Y);

            if (mouseInput.IsOneClick() && Collision.ContainPoint(boxPositioninScreen, m_BoxSize, mouseInput.Position))
            {
                m_BoxAnimationMan.StartAnimation();
                m_PressBtnAnimationMan.StopAnimation();
                m_State = IntroState.BoxAni;
            }

            m_PressBtnAnimationMan.Update(gameTime);
        }

        private void FadeUpdate(GameTime gameTime)
        {
            if (m_FadeHelper.CompleteRender())
            {
                m_State = IntroState.Map;
                m_AnimationMan.StartAnimation();
            }
            else
                m_FadeHelper.Update(gameTime);
        }

        private void MapUpdate(GameTime gameTime)
        {
            if (m_AnimationMan.IsLastIndex())
            {
                ChangeState((IGameState)StateFactory.GetStateFactory().GetObject(StateFactoryID.State_ChoiceMap));
                return;
            }
            m_AnimationMan.Update(gameTime);
        }

        private void BoxUpdate(GameTime gameTime)
        {
            IMouseInputHandler mouseInput = MouseInputHandler.GetInputHandler();

            Vector2 boxPositioninScreen = new Vector2(m_BoxPosition.X, m_BoxPosition.Y - m_ScrollMan.GetCurrentStartPos().Y);

            if (mouseInput.IsOneClick() && Collision.ContainPoint(boxPositioninScreen, m_BoxSize, mouseInput.Position))
            {
                m_BoxAnimationMan.StartAnimation();
                
            }
            if (m_BoxAnimationMan.IsLastIndex())
            {
                m_State = IntroState.Scroll;
                m_BoxAnimationMan.StopAnimation();
            }
            else
                m_BoxAnimationMan.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            SpriteBatch sb = m_GraphicData.GetSpriteBatch();

            if (m_State != IntroState.Map)
            {
                int tempCurrentY = (int)m_ScrollMan.GetCurrentStartPos().Y;

                m_ScrollMan.Draw(sb);
                m_BoxSprite.Draw(sb, new EntityInfo(m_BoxPosition.X, m_BoxPosition.Y - (int)(tempCurrentY), (int)m_BoxSize.X, (int)m_BoxSize.Y));

                if (m_State == IntroState.Fade_Out)
                    m_FadeSprite.Draw(sb, new EntityInfo(0, 0, 800, 480));
                else if( m_State == IntroState.BoxPress)
                {
                    m_BoxPressBtn.Draw(sb, new EntityInfo( new Vector2(m_BoxPressInfo.mPosition.X, m_BoxPressInfo.mPosition.Y - (int)(tempCurrentY)), m_BoxPressInfo.mSize));
                }
            }
            else 
            {
                m_MapSprite.Draw(sb, new EntityInfo(0, 0, 800, 480));
            }

            m_SkipSprite.Draw(sb, m_SkipInfo);
            base.Draw(gameTime);
        }

    }
}
